
//将十六进制数据输出到a.bin文件里
#include <stdio.h>  
#include <string.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <errno.h>  
#include <unistd.h>
#include <stdbool.h>

bool findend(unsigned char* filedata, int count)
{
    int i;
    for(i=0; i<count; i++)
    {
        if(*(filedata+i) != 0xff)
        return false;
    }
    return true;
}


int main(int argc, char **argv)  
{  
    int fd;
    int prtnum = 1;
    bool sbegin = true; 
    long endprt = 0;
    long beginprt = 0;
    fd = open(argv[1], O_RDONLY);  
    if (fd == -1)  
    {  
        printf("open file open_file_test failed!\n%s\n", strerror(errno));  
        return -1;  
    }
    unsigned char buf[10];
    unsigned char buftmp[1];  
    int i;
    i=read(fd, buf, 10);
    if(i != 10) {printf("open file error\n"); close(fd);return -1;}
    endprt+=i-1;
    while(1)
    {
        i=read(fd, buftmp, 1);
        if (i == 0)  {printf("end of file\n"); break;}
        else if (i < 0)  {printf("read file err\n"); close(fd);return -1;}
        else
        {
            endprt+=i;
            for(i=0; i<9; i++) buf[i]=buf[i+1];
            buf[9] = buftmp[0];
            
            if(sbegin)
            {
                //find end true
                if(findend(buf, 10))
                {
                    //print the endprt-10
                    printf("\tend addr is %ld\n", endprt-10);
                    printf("\tlenght is %ld\n", endprt-10-beginprt);
                    //sbegin false
                    sbegin = false;
                }
            }
            //if begin is false
            else
            {
                //not end 
                if(!findend(buf, 10))
                {
                    //print the brginprt
                    beginprt = endprt;
                    prtnum++;
                    printf("\tbegin addr is %ld\n", endprt);
                    //sbegin true
                    sbegin = true;
                }
            }
        }
    }
    printf("\tprtnum is %d\n", prtnum);
    close(fd);
    return 0;
} 

